package com.lwj.liwanjia.view.fragment.myindent;


import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.indent.CompleteAdapter;
import com.lwj.liwanjia.base.LazyFragment;
import com.lwj.liwanjia.bean.DeleteIndentBean;
import com.lwj.liwanjia.config.OnClickPosition;
import com.lwj.liwanjia.impl.DeleteIndentView;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.config.OnItemChildClickListener;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.impl.MyIndentView;
import com.lwj.liwanjia.presenter.DeleteIndentPresenter;
import com.lwj.liwanjia.presenter.MyIndentPresenter;
import com.lwj.liwanjia.utils.CancleDialog;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.indent.UnpaidActivity;
import com.lwj.liwanjia.view.activity.myindent.ReturnActivity;
import com.lwj.liwanjia.view.activity.myindent.ShangChengPjActivity;
import com.lwj.liwanjia.view.activity.myindent.ZhuijiaPjActivity;
import com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo.ApplyStatusActivity;
import com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo.TuiKuanActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 已完成
 */
public class CompleteFragment extends LazyFragment implements MyIndentView, DeleteIndentView, PullRefreshLayout.OnRefreshListener {
    @BindView(R.id.complete_rlv)
    RecyclerView completeRlv;
    @BindView(R.id.progress)
    ProgressBar progressBar;
    @BindView(R.id.no_indent)
    TextView noIndent;
    @BindView(R.id.refressh)
    PullRefreshLayout refreshLayout;

    private int deletePostition = 0;//点击位置
    // 标志位，标志已经初始化完成。
    private boolean isPrepared;
    //    private List<String> list = new ArrayList<>();//测试数据
    private CompleteAdapter completeAdapter;
    private List<MyIndentBean.DataBean> list = new ArrayList<>();
    private MyIndentPresenter myIndentPresenter;
    private DeleteIndentPresenter deleteIndentPresenter;//删除订单
    private String token;
    private int cirlceType = 0;
    private CancleDialog dialog;

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        initData();
    }

    private void initData() {
        myIndentPresenter.getIndentData(token, "已完成");
    }

    @Override
    protected void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        completeRlv.setLayoutManager(manager);
        completeAdapter = new CompleteAdapter(getActivity(), list, false);
        completeRlv.setAdapter(completeAdapter);
        myIndentPresenter = new MyIndentPresenter(this);
        deleteIndentPresenter = new DeleteIndentPresenter(this);
        token = SpfUtils.getSpfUtils(getActivity()).getToken();
        refreshLayout.setOnRefreshListener(this);
        listener();
        isPrepared = true;
        lazyLoad();
    }

    private void listener() {
        completeAdapter.setClickPosition(new OnClickPosition() {
            @Override
            public void clickPosition(int position) {
                deletePostition = position;
            }
        });
        completeAdapter.setListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                deletePostition = position;
                Intent intent = new Intent(getActivity(), UnpaidActivity.class);
                intent.putExtra("tradeId", list.get(position).getGoods().getTradeId());
                startActivity(intent);
            }

        });
        completeAdapter.setChildListener(new OnItemChildClickListener() {
            @Override
            public void OnItemChildClick(View view, String status, final int position) {
                deletePostition = position;
                TextView tv = (TextView) view;
                switch (tv.getText().toString()) {
                    case "评价":
                        Intent intent = new Intent(getActivity(), ShangChengPjActivity.class);
                        intent.putExtra("infor", list.get(position).getGoods());
                        startActivity(intent);
                        break;
                    case "追加评价":
                        Intent intent1 = new Intent(getActivity(), ZhuijiaPjActivity.class);
                        intent1.putExtra("infor", list.get(position).getGoods());
                        startActivity(intent1);
                        break;
                    case "删除订单":
                        dialog = new CancleDialog(getActivity(), list.get(position).getGoods().getStatus());
                        dialog.show();
                        dialog.getSure().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                deleteIndentPresenter.deleteIndent(token, list.get(position).getGoods().getTradeId());
                                dialog.dismiss();
                            }
                        });
                        break;
                    case "联系商家":
                        Intent intent2 = new Intent();
                        intent2.setAction(Intent.ACTION_DIAL);
                        intent2.setData(Uri.parse("tel:" + list.get(position).getGoods().getShoptel()));
                        startActivity(intent2);
                        break;
//                    case "在线沟通":
//                        startActivity(new Intent(getActivity(), KefuActivity.class));
//                        break;
                }
            }
        });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_complete;
    }

    @Override
    public void showProgress() {
        if (cirlceType == 0) {
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(getActivity(), "数据加载失败", Toast.LENGTH_SHORT).show();
        refreshLayout.setRefreshing(false);
    }

    /**
     * 删除订单
     *
     * @param deleteBean
     */
    @Override
    public void deleteIndent(DeleteIndentBean deleteBean) {
        if (deleteBean.getState() == 0) {
            Toast.makeText(getActivity(), "访问有误", Toast.LENGTH_SHORT).show();
        } else {
            if (deleteBean.getData().getStatus().equals("删除成功")) {
                list.remove(deletePostition);
                if (list.size() == 0) {
                    completeRlv.setVisibility(View.GONE);
                    noIndent.setVisibility(View.VISIBLE);
                }
                completeAdapter.notifyDataSetChanged();
            } else {
                Toast.makeText(getActivity(), "删除失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 获取所有订单
     *
     * @param myIndentBean
     */
    @Override
    public void getIndentData(MyIndentBean myIndentBean) {
        if (myIndentBean.getState() == 0) {
            Toast.makeText(getActivity(), "访问有误", Toast.LENGTH_SHORT).show();
        } else {
            list.clear();
            list.addAll(myIndentBean.getData());
            if (list.size() > 0) {
                completeRlv.setVisibility(View.VISIBLE);
                noIndent.setVisibility(View.GONE);
                completeAdapter.notifyDataSetChanged();
            } else {
                completeRlv.setVisibility(View.GONE);
                noIndent.setVisibility(View.VISIBLE);
            }
        }
        completeRlv.scrollToPosition(deletePostition);
        cirlceType = 0;
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (myIndentPresenter != null) {
            myIndentPresenter = null;
        }
    }


    @OnClick(R.id.no_indent)
    public void onViewClicked() {
        initData();
    }

    @Override
    public void onRefresh() {
        cirlceType = 1;
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isPrepared && isVisible) {
            initData();
        }
    }
}
